﻿#include "fsaddrelatedialog.h"
#include "ui_fsaddrelatedialog.h"
#include <QSqlQuery>

QString FSaddrelateDialog::PID;
FSaddrelateDialog::FSaddrelateDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FSaddrelateDialog)
{
    ui->setupUi(this);
    ui->dtTime->setDate(QDate::currentDate());
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("添加信息状态：");
    connect(this,SIGNAL(sendD(QString)),parent,SLOT(showrow(QString)));
}

FSaddrelateDialog::~FSaddrelateDialog()
{
    delete ui;}

void FSaddrelateDialog::on_btnCancel_clicked()
{
    this->close();
}

void FSaddrelateDialog::receiveID(QString id)
{PID=id;}

void FSaddrelateDialog::on_btnOk_clicked()
{

    strName = ui->edtName->text();
    strDuty = ui->edtDuty->text();
    if(ui->cbBxIDType->currentIndex() != 7){
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+1);
    }else{
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+2);
    }
    strID = ui->edtID->text();
    strDutyTime = ui->dtTime->date().toString("yyyy-MM-dd");//任职时间
    rowNum = ui->edtMany->text();
    const char *s = rowNum.toUtf8().data();
    while(*s && *s>='0' && *s<='9')s++;//bool(*s)返回false为数字
    if(strName.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入姓名！");
    }else if(strName.size() > 50){//50
        ui->status->setText("添加信息状态：姓名信息输入有误！");
    }else if(strDuty.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入职务！");
    }else if(strDuty.size() > 80){//80
        ui->status->setText("添加信息状态：职务信息输入有误！");
    }else if(strID.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入证件号！");
    }else if(strID.size() > 18){//18
        ui->status->setText("添加信息状态：证件号信息输入有误！");
    }else if(rowNum.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入卷数！");
    }else if(bool(*s)){
        ui->status->setText("添加信息状态：卷数信息输入有误！");
    }else{
        QString strInsert=QString("insert into FileSbmtRow values (null,'%1','%2',%3,'%4','%5','%6',%7); ")
                .arg(PID).arg(strName).arg(strIDType).arg(strID).arg(strDuty).arg(strDutyTime)
                .arg(rowNum);
        QSqlQuery query;
        if(query.exec(strInsert)){
            ui->status->setText("添加信息状态：信息登记成功！");
            sendD(PID);
            this->close();
        }else{
            ui->status->setText("添加信息状态：信息登记失败！");
        }
    }
}
